package com.wiz.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.web.multipart.MultipartFile;

import com.wiz.common.Constant;

public class FileUtil {
	private static List<File>filelist=new ArrayList<File>();
	
	
	 //文件上传  
    public static String uploadFile(MultipartFile file,String directory,String id,int type) throws IOException {  
        long modelNum = Long.valueOf(id)%10;
        File tempFile = new File(AddressUtil.defultAddress()+File.separator+directory+File.separator+modelNum,
        		id + new Date().getTime()+type +file.getOriginalFilename().substring(file.getOriginalFilename().indexOf(".")));  
        if (!tempFile.getParentFile().exists()) {  
            tempFile.getParentFile().mkdirs();  
        }  
        if (!tempFile.exists()) {  
            tempFile.createNewFile();  
        }  
        file.transferTo(tempFile);  
        return File.separator+directory+File.separator+modelNum+File.separator + tempFile.getName();  
    }  
    
	public static void copy(File src, File dst) {
		InputStream in = null;
		OutputStream out = null;
		int size = 2048;
		try {
			if (dst.exists()) {
				out = new BufferedOutputStream(new FileOutputStream(dst, true),size);
			} else {
				out = new BufferedOutputStream(new FileOutputStream(dst),size);
			}
			in = new BufferedInputStream(new FileInputStream(src), size);

			byte[] buffer = new byte[size];
			int len = 0;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != out) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/**
     * 删除指定文件夹下所有文件
     * @param path 文件夹完整绝对路径
     */
    public static boolean delAllFile(String path) throws Exception {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                //先删除文件夹里面的文件
                delAllFile(path + "/" + tempList[i]);
                //再删除空文件夹
                delFolder(path + "/" + tempList[i]);
                flag = true;
            }
        }
        return flag;
    }
    /**
     * 删除文件夹
     * @param folderPath folderPath 文件夹完整绝对路径
     */
    public static void delFolder(String folderPath) throws Exception {
        //删除完里面所有内容
        delAllFile(folderPath);
        String filePath = folderPath;
        filePath = filePath.toString();
        File myFilePath = new File(filePath);
        //删除空文件夹
        myFilePath.delete();
    }
//    public static void main(String[] args) {
//		FileUtil.delFolder("d:\GSP\imager\temporary\20150330\c93e6d3c-1d41-425c-8919-0adeb58f6cdc.zip");
//	}
	//删除目录下的文件
	public static boolean deleteFile(File file)throws Exception {
		File[]files = file.listFiles();
		for (File file2 : files) {
			if(file2.isDirectory()){
				if(!deleteFile(file2)){
					return false;
				}
			}else{
				if(!file2.delete()){
					return false;
				}
			}
		}
		return file.delete();
	}
	
	public static List<File> addFile(File file){
		File[] list = file.listFiles();
		
		try {
			for (File file2 : list) {
				if(file2.isDirectory()){
					FileUtil.deleteFile(file2);
				}
				filelist.add(file2);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return filelist;
	}
	

}
